package a03_哈希;

import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 * a04_快乐数复习6
 * </p>
 *
 * @author flyduck
 * @since 2025/4/15
 */
public class a04_快乐数复习6 {

    public static void main(String[] args) {
        a04_快乐数复习6 test = new a04_快乐数复习6();
        System.out.println(test.getNext(129));
    }

    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        while (true){
            //计算下一个
            n = getNext(n);
            //判断之前是否存在
            if(n == 1){
                return true;
            } else if (set.contains(n)) {
                return false;
            }else {
                set.add(n);
            }
        }
    }

    //129=>86
    //129 % 10 = 12 9
    //12 % 10 = 1 2
    //1 % 10 = 0 1
    private int getNext(int n){
        int result = 0;
        while (n > 0){
            result += (n % 10) * (n % 10);
            n = n / 10;
        }
        return result;
    }
}
